package es.ucm.fdi.healthpred.vista;

import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JTextField;

/**
 * Campo de texto que s&oacute;lo admite d&iacute;gitos y selecciona todo el
 * texto al ganar el foco.
 * 
 * @since 1.0
 * 
 * @version 1.0
 * 
 * @author &Aacute;lvaro G&oacute;mez Porris
 * @author V&iacute;ctor L&oacute;pez Monte
 * @author Carlos Rodr&iacute;guez Colliga
 * 
 */
public class JTextFieldDigitosFoco extends JTextField {

	private static final long serialVersionUID = 1L;

	/**
	 * 
	 */
	public JTextFieldDigitosFoco() {
		super();
		restringeDigitos();
	}

	/**
	 * @param valor
	 */
	public JTextFieldDigitosFoco(Integer valor) {
		super(valor);
		restringeDigitos();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.swing.JFormattedTextField#processFocusEvent(java.awt.event.FocusEvent
	 * )
	 */
	protected void processFocusEvent(FocusEvent e) {
		super.processFocusEvent(e);
		if (e.getID() == FocusEvent.FOCUS_GAINED)
			selectAll();
	}

	/* PRIVADO */
	private void restringeDigitos() {
		addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent e) {
				char caracter = e.getKeyChar();
				if (((caracter < '0') || (caracter > '9'))
						&& (caracter != KeyEvent.VK_BACK_SPACE)) {
					e.consume(); // ignorar el evento de teclado
				}
			}
		});
	}

}
